package org.jeecg.modules.slider;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.config.shiro.IgnoreAuth;
import org.jeecg.modules.company.entity.JKSliderVo;
import org.jeecg.modules.company.entity.JkCompany;
import org.jeecg.modules.company.service.IJkCompanyService;
import org.jeecg.modules.papers.entity.JkPapers;
import org.jeecg.modules.papers.service.IJkPapersService;
import org.jeecg.modules.paten.entity.JkPatentInfo;
import org.jeecg.modules.paten.service.IJkPatentInfoService;
import org.jeecg.modules.product.entity.JkProduct;
import org.jeecg.modules.product.service.IJkProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Tag(name="前端jk_product企业接口")
@RestController
@RequestMapping("/api/slider")
@Slf4j
public class SliderWebController {

    @Autowired
    private IJkCompanyService jkCompanyService;

    @Autowired
    private IJkPapersService jkPapersService;

    @Autowired
    private IJkPatentInfoService jkPatentInfoService;

    @Autowired
    private IJkProductService jkProductService;

    @Operation(summary="jk_slider-列表查询")
    @GetMapping(value = "/list")
    @IgnoreAuth
    public Result<List<JKSliderVo>> queryPageList(HttpServletRequest req) {

        String openSlider ="1";
        List<JKSliderVo> mergedList = new ArrayList<>();  // 初始化并复制 patentList
        LambdaQueryWrapper<JkCompany> companyQueryWrapper = new LambdaQueryWrapper<>();
        companyQueryWrapper.eq(JkCompany::getSlider, openSlider)
                .select(JkCompany::getId,
                        JkCompany::getLogo,
                        JkCompany::getName,
                        JkCompany::getDescription); // 其他需要的字段;
        //List<JkCompany> companyList = jkCompanyService.list(companyQueryWrapper);
        List<JKSliderVo> companyList = jkCompanyService.list(companyQueryWrapper).stream()
                .map(company -> {
                    JKSliderVo vo = new JKSliderVo();
                    vo.setId(company.getId());
                    vo.setTitle(company.getName());
                   vo.setContent(company.getDescription());
                   vo.setLogo(company.getLogo());
                    vo.setType("1");
                    return vo;
                })
                .collect(Collectors.toList());
        mergedList.addAll(companyList);

        LambdaQueryWrapper<JkPapers> paperQueryWrapper = new LambdaQueryWrapper<>();
        paperQueryWrapper.eq(JkPapers::getSlider, openSlider)
                .select(JkPapers::getId,
                        JkPapers::getImageurl,
                        JkPapers::getPaperTitle,
                        JkPapers::getPaperAbstract);

        List<JKSliderVo> papersList = jkPapersService.list(paperQueryWrapper).stream()
                .map(paper -> {
                    JKSliderVo vo = new JKSliderVo();
                    vo.setId(paper.getId());
                    vo.setTitle(paper.getPaperTitle());
                   vo.setLogo(paper.getImageurl());
                   vo.setContent(paper.getPaperAbstract());
                   vo.setType("2");
                    return vo;
                })
                .collect(Collectors.toList());

        mergedList.addAll(papersList);
        LambdaQueryWrapper<JkProduct> productLambdaQueryWrapper = new LambdaQueryWrapper<>();
        productLambdaQueryWrapper.eq(JkProduct::getSlider, openSlider);

        List<JKSliderVo> productList = jkProductService.list(productLambdaQueryWrapper).stream()
                .map(product -> {
                    JKSliderVo vo = new JKSliderVo();
                    vo.setId(product.getId());
                    vo.setTitle(product.getProductname());
                    vo.setLogo(product.getImageurl());
                    vo.setContent(product.getProductdetails());
                    vo.setType("3");
                    return vo;
                })
                .collect(Collectors.toList());
        mergedList.addAll(productList);

        LambdaQueryWrapper<JkPatentInfo> patentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        patentInfoLambdaQueryWrapper.eq(JkPatentInfo::getSlider, openSlider)
                .select(JkPatentInfo::getId,
                        JkPatentInfo::getPatentAbstract,
                        JkPatentInfo::getImageurl,
                        JkPatentInfo::getPatentTitle);

        List<JKSliderVo> patenList = jkPatentInfoService.list(patentInfoLambdaQueryWrapper).stream()
                .map(paten -> {
                    JKSliderVo vo = new JKSliderVo();
                    vo.setId(paten.getId());
                    vo.setTitle(paten.getPatentTitle());
                    vo.setLogo(paten.getImageurl());
                    vo.setContent(paten.getPatentAbstract());
                    vo.setType("4");
                    return vo;
                })
                .collect(Collectors.toList());

        mergedList.addAll(patenList);
        return Result.OK(mergedList);
    }
}
